package com.gm.wj.service;

import com.gm.wj.config.RedisConfig;
import com.gm.wj.dao.SysTeacherDAO;
import com.gm.wj.entity.SysTeacher;
import com.gm.wj.redis.RedisService;
import com.gm.wj.util.MyPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Set;

/**
 * @program: wj
 * @description: 教师
 * @author: Huang_shengjun
 * @create: 2020-09-16 22:23
 **/
@Slf4j
@Service
public class SysTeacherService {

    @Autowired
    SysTeacherDAO sysTeacherDAO;
    @Autowired
    RedisService redisService;
    @Autowired
    UserService userService;

    public MyPage list(int page, int size) {
        MyPage<SysTeacher> teachers;
        // list缓存
        String key = "teacherpage:" + page;
        Object teacherPageCache = redisService.get(key);

        // 如果为空，就再查询一次，写入redis缓存
        if (teacherPageCache == null) {
            Sort sort = Sort.by(Sort.Direction.DESC, "id");
            Page<SysTeacher> teacherInDb = sysTeacherDAO.findAll(PageRequest.of(page, size, sort));
            teachers = new MyPage<>(teacherInDb);
            // 设置redis过期时间  5s
            redisService.set(key, teachers, RedisConfig.DATA_REDIS_KEY_TIME);
        } else {
            teachers = (MyPage<SysTeacher>) teacherPageCache;
        }
        return teachers;
    }

    public SysTeacher findById(int id) {
        SysTeacher sysTeacher;
        String key = "teacher:" + id;
        Object teacherCache = redisService.get(key);

        if (teacherCache == null) {
            sysTeacher = sysTeacherDAO.findById(id);
            redisService.set(key, sysTeacher,RedisConfig.DATA_REDIS_KEY_TIME);
        } else {
            sysTeacher = (SysTeacher) teacherCache;
        }
        return sysTeacher;
    }

    @Transactional
    public int addOrUpdate(SysTeacher sysTeacher) {
        sysTeacherDAO.save(sysTeacher);

        redisService.delete("teacher" + sysTeacher.getId());
        Set<String> keys = redisService.getKeysByPattern("teacherpage*");
        redisService.delete(keys);
        int statu = userService.registerTea(sysTeacher);
        if (statu != 1) {
            log.info("--进入回滚--" );
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
        return 1;
    }

    public void delete(int id) {
        sysTeacherDAO.deleteById(id);

        redisService.delete("teacher:" + id);
        Set<String> keys = redisService.getKeysByPattern("teacherpage*");
        redisService.delete(keys);
    }

    public void updateStatus(SysTeacher sysTeacher) {
        SysTeacher teacherInDB = sysTeacherDAO.findById(sysTeacher.getId());
        teacherInDB.setEnabled(sysTeacher.isEnabled());
        sysTeacherDAO.save(teacherInDB);
    }


    public List<SysTeacher> listByQueryKey(String queryKey1, String queryKey2, String queryKey3) {

        // 多条件模糊查询
//        queryKey1 = (queryKey1 == null && queryKey1 == "" ) ? null : "" + queryKey1 + ""; // booble值  直接用等于  不用like了
        queryKey1 = (queryKey1 == null) ? null : "%" + queryKey1 + "%";
        queryKey2 = (queryKey2 == null) ? null : "%" + queryKey2 + "%";
        queryKey3 = (queryKey3 == null) ? null : "%" + queryKey3 + "%";
        List<SysTeacher> sysTeachers = sysTeacherDAO.find(queryKey1, queryKey2, queryKey3);

        return sysTeachers;
    }

    public int UseraddTea(SysTeacher sysTeacher) {
        sysTeacherDAO.save(sysTeacher);
        return 1;
    }

    public SysTeacher update(SysTeacher sysTeacher) {
        return sysTeacherDAO.save(sysTeacher);
    }
}
